﻿using System;
using System.Collections.Generic;
using NHibernate;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Models
{
    public class DashBoardModel
    {
        private ISession _session;

        public IList<TimeLine> GetTimeLines()
        {
            IList<TimeLine> timeLines;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from TimeLine where ProjectId='" + Client.Instance.CurrentProjectId +
                                         "' ORDER BY Time DESC;");
                timeLines = query.List<TimeLine>();
            }
            return timeLines;
        }

        public TimeLine GetTimeLineById(Guid id)
        {
            TimeLine timeLine;
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    timeLine = _session.Get<TimeLine>(id);
                    tran.Commit();
                }
            }
            return timeLine;
        }

        public bool NewTimeLine(TimeLine timeLine)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Save(timeLine);
                    tran.Commit();
                }
            }
            return true;
        }

        public void UpdateTimeLine(TimeLine timeLine)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Update(timeLine);
                    tran.Commit();
                }
            }
        }

        public void DeleteTimeLine(TimeLine timeLine)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    try
                    {
                        _session.Delete(timeLine);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
            }
        }
    }
}